package com.wandian.weather.mvvm.realweather.realtimefragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mylib.libcore.AppContext;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.weather.bean.WeatherInformationBean;
import com.wandian.weather.databinding.WeatherFragmentRealTeimWeatherBinding;
import com.wandian.weather.tools.WeatherUtil;

/**
 * @author Administrator
 * @date 2019/12/16-13:30
 * @description 实时天气
 **/
public class RealtimeWeatherFragment extends BaseMVVMFragment<RealtimeWeatherViewModel> {

    private WeatherFragmentRealTeimWeatherBinding dataBinding;
    private String title;

    public static RealtimeWeatherFragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString("title", title);
        RealtimeWeatherFragment fragment = new RealtimeWeatherFragment();
        fragment.setArguments(args);
        return fragment;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = WeatherFragmentRealTeimWeatherBinding.inflate(inflater);
        dataBinding.setLifecycleOwner(this);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        title = getArguments().getString("title");
        dataBinding.setViewModel(mViewModel);
        initViewData();
    }

    private void initViewData(){
        try {
            String weatherJson = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("WeatherInformationBean");
            if(UIUtils.isEmpty(weatherJson)){
                return;
            }
            WeatherInformationBean informationBean = JsonUtil.parseJsonToBean(weatherJson, WeatherInformationBean.class);
            if (informationBean != null) {
                WeatherInformationBean.BaseBean baseBean = informationBean.getBase();
                dataBinding.tvTemperature.setText(baseBean.getStemp() + "°");
                dataBinding.tvTemperatureTips.setText(baseBean.getWeather());
                dataBinding.tvTemperatureWind.setText(informationBean.getHourly_description());
                dataBinding.tvRltwWind.setText(baseBean.getWD());
//                Log.e("husong", "----------->>>>ws="+baseBean.getWS()+"  pha="+baseBean.getPres());
                dataBinding.tvRltwWindLevel.setText(baseBean.getWS());
                dataBinding.tvRltwHumidity.setText(baseBean.getSD());

                dataBinding.imgTem.setImageResource(WeatherUtil.getWeatherStateImage(baseBean.getImg_num(), false));
                float pha = Float.parseFloat(baseBean.getPres())/100;
                int pha1 = (int) pha;
                dataBinding.tvRltwHpa.setText(pha1+" hPa");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
